package api

import (
	"context"
	"myproject/backend/types"
	"myproject/backend/utils"
)

type DeviceApi struct {
	Ctx context.Context
}

func NewDeviceApi() *DeviceApi {
	return &DeviceApi{}
}

func (d *DeviceApi) ListMyDevice(req types.WxDeviceTagQuery) types.WxListDeviceResp {
	resp := types.WxListDeviceResp{}
	body := map[string]any{}
	if req.SubordinateOrganizeId != 0 {
		body["subordinateOrganizeId"] = req.SubordinateOrganizeId
	}
	if req.OrganizeId != 0 {
		body["organizeId"] = req.OrganizeId
	}
	if req.IsBinding != 0 {
		body["isBinding"] = req.IsBinding
	}
	body["page"] = req.Page
	body["pageSize"] = req.PageSize
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/wx/myDevice/list",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (d *DeviceApi) UpdateDeviceInfo(req types.DeviceInfoReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["deviceName"] = req.DeviceName
	body["deviceSn"] = req.DeviceSn
	body["outCode"] = req.OutCode
	body["categoryId"] = req.CategoryId
	body["brandId"] = req.BrandId
	body["typeId"] = req.TypeId
	body["typeName"] = req.TypeName
	body["organizeId"] = req.OrganizeId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/wx/custom/updateDeviceInfo",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 注册设备
func (d *DeviceApi) RegisterDevice(req types.WxHeadClassRegisterDeviceInfo) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["outCode"] = req.OutCode
	body["categoryId"] = req.CategoryId
	body["categoryName"] = req.CategoryName
	body["deviceName"] = req.DeviceName
	body["typeId"] = req.TypeId
	body["typeName"] = req.TypeName
	body["brandName"] = req.BrandName
	body["brandId"] = req.BrandId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/wx/custom/registerDeviceByCustom",
		Body:   body,
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 获取国家列表
func (d *DeviceApi) GetCountryList() types.CountryInfoListResp {
	resp := types.CountryInfoListResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/baseinfo/base/getCountryList",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 获取激活码
func (d *DeviceApi) DeviceRegister(req types.DeviceRegisterReq) types.DeviceActivateInfoResp {
	resp := types.DeviceActivateInfoResp{}
	body := map[string]any{}

	if req.OutCode != "" {
		body["outCode"] = req.OutCode
	}
	if req.ProductId != 0 {
		body["productId"] = req.ProductId
	}
	if req.DeviceRegistBy != 0 {
		body["deviceRegistBy"] = req.DeviceRegistBy
	}
	if req.From != "" {
		body["from"] = req.From
	}
	if req.ActivateResultUrl != "" {
		body["activateResultUrl"] = req.ActivateResultUrl
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/devser/registe",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// updateDeviceName
func (d *DeviceApi) UpdateDeviceName(req types.UpdateDeviceNameReq) types.DeviceActivateInfoResp {
	resp := types.DeviceActivateInfoResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["deviceName"] = req.DeviceName
	body["outCode"] = req.OutCode
	body["status"] = req.Status
	body["isBinding"] = req.IsBinding
	body["deviceSn"] = req.DeviceSn
	body["bindingOrganization"] = req.BindingOrganization
	body["brandId"] = req.BrandId
	body["goodsId"] = req.GoodsId
	body["brandName"] = req.BrandName
	body["categoryId"] = req.CategoryId
	body["categoryName"] = req.CategoryName
	body["typeId"] = req.TypeId
	body["typeName"] = req.TypeName
	body["dealerCompanyName"] = req.DealerCompanyName
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/wx/custom/updateDeviceName",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询合作医生
func (d *DeviceApi) ListCooperativeDoctorsAll() types.CooperativeDoctorListResp {
	resp := types.CooperativeDoctorListResp{}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "GET",
		Suffix: "/report/cooperativeDoctor/listCooperativeDoctorsAll",
		Resp:   &resp,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 查询授权码列表
func (d *DeviceApi) ListMqttAuthCodes(req types.MqttAuthCodeListReq) types.MqttAuthCodeListResp {
	resp := types.MqttAuthCodeListResp{}
	body := map[string]any{}
	if req.Page != 0 {
		body["page"] = req.Page
	}
	if req.Size != 0 {
		body["size"] = req.Size
	}
	if req.DeviceSn != "" {
		body["deviceSn"] = req.DeviceSn
	}
	if req.DoctorUsername != "" {
		body["doctorUsername"] = req.DoctorUsername
	}
	if req.DoctorNickname != "" {
		body["doctorNickname"] = req.DoctorNickname
	}
	if req.AuthExpireTime != 0 {
		body["authExpireTime"] = req.AuthExpireTime
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/mqttAuthCode/listMqttAuthCodes",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// updateMqttAuthExpireTime
func (d *DeviceApi) UpdateMqttAuthExpireTime(req types.MqttAuthCodeUpdateReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["authExpireTime"] = req.AuthExpireTime
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/mqttAuthCode/updateMqttAuthExpireTime",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// deleteMqttAuthCode
func (d *DeviceApi) DeleteMqttAuthCode(req types.MqttAuthCodeUpdateReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/mqttAuthCode/deleteMqttAuthCode",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (d *DeviceApi) UpdateMqttAuthCodeStatus(req types.MqttAuthCodeUpdateStatusReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["status"] = req.Status
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/mqttAuthCode/updateMqttAuthStatus",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// createMqttAuthCode
func (d *DeviceApi) CreateMqttAuthCode(req types.CreateMqttAuthCodeReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["authExpireTime"] = req.AuthExpireTime
	body["brandName"] = req.BrandName
	body["categoryName"] = req.CategoryName
	body["deviceSn"] = req.DeviceSn
	body["doctorNickname"] = req.DoctorNickname
	body["doctorUsername"] = req.DoctorUsername
	body["outCode"] = req.OutCode
	body["typeName"] = req.TypeName
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/mqttAuthCode/createMqttAuthCode",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 数据桥列表
func (d *DeviceApi) ListDataBridge(req types.ListDataBridgeReq) types.ListDataBridgeResp {
	resp := types.ListDataBridgeResp{}
	params := map[string]any{}
	if req.Page != 0 {
		params["page"] = req.Page
	}
	if req.Size != 0 {
		params["size"] = req.Size
	}
	if req.SrcOutCode != "" {
		params["srcOutCode"] = req.SrcOutCode
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/report/deviceCheckRecord/openApi/v1/dataBridgeList",
		Resp:        &resp,
		QueryParams: params,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// 数据桥绑定
func (d *DeviceApi) DataBridgeBind(req types.DateBridgeBindReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["distDeviceSn"] = req.DistDeviceSn
	body["distOutCode"] = req.DistOutCode
	body["isOk"] = req.IsOk
	body["messageTypes"] = req.MessageTypes
	body["srcDeviceSn"] = req.SrcDeviceSn
	body["srcOutCode"] = req.SrcOutCode
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/deviceCheckRecord/openApi/v1/dataBridgeBind",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (d *DeviceApi) DataBridgeUnBind(req types.DateBridgeBindReq) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["distDeviceSn"] = req.DistDeviceSn
	body["distOutCode"] = req.DistOutCode
	body["isOk"] = req.IsOk
	body["messageTypes"] = req.MessageTypes
	body["srcDeviceSn"] = req.SrcDeviceSn
	body["srcOutCode"] = req.SrcOutCode
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/report/deviceCheckRecord/openApi/v1/dataBridgeUnbind",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

func (d *DeviceApi) ListDeviceByOutCodeAndName(req types.ListDeviceByOutCodeAndNameReq) types.WxListDeviceResp {
	resp := types.WxListDeviceResp{}
	query := map[string]any{}
	if req.Page != 0 {
		query["page"] = req.Page
	}
	if req.PageSize != 0 {
		query["pageSize"] = req.PageSize
	}
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method:      "GET",
		Suffix:      "/bms/wx/custom/listDeviceByOutCodeAndName",
		Resp:        &resp,
		QueryParams: query,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}

// unbindDevice
func (d *DeviceApi) UnbindDevice(req types.WxCustomDeviceId) types.JsResp {
	resp := types.JsResp{}
	body := map[string]any{}
	body["id"] = req.Id
	body["organizeId"] = req.OrganizeId
	err := utils.HttpRequest(utils.HttpRequestOptions{
		Method: "POST",
		Suffix: "/bms/wx/custom/unbindDevice",
		Resp:   &resp,
		Body:   body,
	})
	if err != nil {
		resp.Code = types.FAIL
		resp.Msg = err.Error()
		return resp
	}
	return resp
}
